package com.lanou3g.dllo.baidumusic.login;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lanou3g.dllo.baidumusic.R;
import com.lanou3g.dllo.baidumusic.base.BaseActivity;
import com.lanou3g.dllo.baidumusic.base.MyApp;
import com.lanou3g.dllo.baidumusic.main.MainActivity;

import cn.bmob.v3.BmobUser;

/**
 * Created by dllo on 16/7/6.
 */
public class UserActivity extends BaseActivity implements View.OnClickListener {

    private Button loginRightNow;
    private TextView userUserName, userExitLogin;
    private RelativeLayout loginLayout;
    private LinearLayout exitLoginLayout;

    @Override
    public int setLayout() {
        return R.layout.activity_user;
    }

    @Override
    public void initView() {
        loginRightNow = (Button) findViewById(R.id.login_rightnow);
        userUserName = (TextView) findViewById(R.id.user_username);
        userExitLogin = (TextView) findViewById(R.id.user_exitlogin);
        loginLayout = (RelativeLayout) findViewById(R.id.login_layout);
        exitLoginLayout = (LinearLayout) findViewById(R.id.exitlogin_layout);

        BmobUser bmobUser = BmobUser.getCurrentUser(this);
        if (bmobUser == null) {
            loginLayout.setVisibility(View.VISIBLE);
            exitLoginLayout.setVisibility(View.GONE);
        } else {
            exitLoginLayout.setVisibility(View.VISIBLE);
            loginLayout.setVisibility(View.GONE);
            userUserName.setText(bmobUser.getUsername());
        }

        loginRightNow.setOnClickListener(this);
        userExitLogin.setOnClickListener(this);
        loginLayout.setOnClickListener(this);
        exitLoginLayout.setOnClickListener(this);

    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            // 立即登录
            case R.id.login_rightnow:
                Intent intent = new Intent(UserActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
                break;
            // 退出登录
            case R.id.user_exitlogin:
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setIcon(R.mipmap.mm3).setTitle("退出登录").setMessage("是否退出登录?");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        BmobUser bmobUser = new BmobUser();
                        bmobUser.logOut(MyApp.context);
                        loginLayout.setVisibility(View.VISIBLE);
                        exitLoginLayout.setVisibility(View.GONE);
                        // 刷新我的界面我喜欢的单曲的歌曲数
                        Intent intent = new Intent(getPackageName() + ".REFRESHMYFAVORITE");
                        sendBroadcast(intent);
                        Intent intent1 = new Intent(UserActivity.this, MainActivity.class);
                        startActivity(intent1);
                        finish();
                    }
                });

                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });

                builder.show();
                break;
        }

    }
}
